<?php
	$Link = $Link2 = '';
	$dir='cache/';
	if(!is_dir($dir)) mkdir($dir,0755,true);
	$Link = $dir.'ExchangeRates.xml';
	$Link2 = 'http://vietcombank.com.vn/ExchangeRates/ExrateXML.aspx';
	$content = @file_get_contents($Link2);
	if($content==''){
		$content = @file_get_contents($Link);
	}else{
		copy($Link2,$Link);
	}

	if($content!='' and preg_match_all('/Exrate CurrencyCode="(.*)" CurrencyName="(.*)" Buy="(.*)" Transfer="(.*)" Sell="(.*)"/',$content,$matches) and count($matches)>0){

		$exchange_rates=array(

				'USD'=>array()

				,'EUR'=>array()

				,'GBP'=>array()

				,'HKD'=>array()

				,'JPY'=>array()

				,'CHF'=>array()

				,'AUD'=>array()

				,'CAD'=>array()

				,'SGD'=>array()

				,'THB'=>array()

		);

		foreach($matches[1] as $key=>$value){

			if(isset($exchange_rates[$value])){

				$exchange_rates[$value]=array(

						'id'=>$value

						,'name'=>$matches[2][$key]

						,'buy'=>$matches[3][$key]

						,'transfer'=>$matches[4][$key]

						,'sell'=>$matches[5][$key]

				);

			}

		}
	}

	function ajax_money(){
		$Link = $Link2 = '';
		$dir='cache/';
		if(!is_dir($dir)) mkdir($dir,0755,true);
		$Link = $dir.'ExchangeRates.xml';
		$Link2 = 'http://vietcombank.com.vn/ExchangeRates/ExrateXML.aspx';
		$content = @file_get_contents($Link2);
		if($content==''){
			$content = @file_get_contents($Link);
		}else{
			copy($Link2,$Link);
		}

		if($content!='' and preg_match_all('/Exrate CurrencyCode="(.*)" CurrencyName="(.*)" Buy="(.*)" Transfer="(.*)" Sell="(.*)"/',$content,$matches) and count($matches)>0){
			$exchange_rates=array(
					'USD'=>array()
					,'EUR'=>array()
					,'GBP'=>array()
					,'HKD'=>array()
					,'JPY'=>array()
					,'CHF'=>array()
					,'AUD'=>array()
					,'CAD'=>array()
					,'SGD'=>array()
					,'THB'=>array()
			);
			foreach($matches[1] as $key=>$value){
		  if(isset($exchange_rates[$value])){
		  	$exchange_rates[$value]=array(
		  			'id'=>$value
		  			,'name'=>$matches[2][$key]
		  			,'buy'=>$matches[3][$key]
		  			,'transfer'=>$matches[4][$key]
		  			,'sell'=>$matches[5][$key]
		  	);
		  }
			}
			return $exchange_rates;
		}
	}

	echo "<tr><td>";
	if(empty($_SESSION['lang'])) echo "Name</td><td>";

	else echo "Tên</td><td>";

	if(empty($_SESSION['lang'])) echo "Sell</td><td>";

	else echo "Bán</td><td>";

	if(empty($_SESSION['lang'])) echo "Buy</td></tr>";

	else echo "Mua</td></tr>";

	$data_money = ajax_money();
	foreach($data_money as $id=>$item){
	    echo "<tr><td>".$id."</td>";
	    echo "<td>".number_format($item['sell'], 2)."</td>";
	    echo "<td>".number_format($item['buy'], 2)."</td></tr>";
	}
?>
